use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::api::API;
use crate::utils::http_client_app::HttpAppClient;

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AccountInfoListResp {
    pub zh: Option<String>,
    // 账号
    pub zh_type: Option<String>,
    // 账号类型
    pub serid: Option<String>,           // 区服ID

    #[serde(skip_serializing_if = "Option::is_none")]
    pub zh_cfg: Option<Value>,           // 账号配置(任意JSON对象或None)

    pub zhw_hid: Option<String>,
    // zhw货架
    pub zhw_zt: Option<String>,
    // zhw状态
    pub zhw_money: Option<String>,       // zhw单价

    pub uhz_hid: Option<String>,
    // uhz货架
    pub uhz_zt: Option<String>,
    // uhz状态
    pub uhz_money: Option<String>,       // uhz单价

    pub zz_hid: Option<String>,
    // zz货架
    pub zz_zt: Option<String>,
    // zz状态
    pub zz_money: Option<String>,        // zz单价
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AccountInfoWrap {
    pub acclist: Vec<AccountInfoListResp>,
}

pub async fn get_account_info_list() -> Option<Vec<AccountInfoListResp>> {
    let client = HttpAppClient::new(30).unwrap();

    match client.post::<AccountInfoWrap>(API::HidList.url().as_str(), None).await {
        Ok(Some(data)) => {
            log::info!("成功获取账号列表，数量: {}", data.acclist.len());
            Some(data.acclist)
        }
        Ok(None) => {
            log::warn!("响应为空，返回空列表");
            Some(Vec::new())
        }
        Err(err) => {
            log::error!("获取账号列表失败: {:?}", err);
            Some(Vec::new())
        }
    }
}